/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_bed_connecttype;
import org.testis.orm.classes.T_bed_connecttypeCriteria;
import org.testis.orm.classes.T_bed_serialconnection;
import org.testis.orm.classes.T_bed_serialconnectionCriteria;
import org.testis.orm.classes.T_bed_serialconnectionFactory;

/**
 * 
 * @author selcuk
 */
public class SerialConnectionDataHandler extends AtofOrmFlyweightFactory {

    private static SerialConnectionDataHandler m_reference;

    private SerialConnectionDataHandler() {
	loadSerialConnections();
    }

    public static SerialConnectionDataHandler newInstance() {
	if (m_reference == null) {
	    m_reference = new SerialConnectionDataHandler();
	}
	return m_reference;
    }

    public Collection getSerialConnections() {
	return FLY_WEIGHT_DATA.values();
    }

    public T_bed_serialconnection[] getSerialConnectionArray() {
	Collection col = getSerialConnections();
	T_bed_serialconnection[] result = new T_bed_serialconnection[col.size()];
	col.toArray(result);
	return result;
    }

    public LinkedList<T_bed_serialconnection> getSerialConnectionList() {
	Collection col = getSerialConnections();
	return new LinkedList<T_bed_serialconnection>(col);
    }

    public HashMap getSerialConnectionMap() {
	return new HashMap(FLY_WEIGHT_DATA);
    }

    public int getNumberOfSerialConnections() {
	return FLY_WEIGHT_DATA.size();
    }

    public T_bed_serialconnection getSerialConnectionById(int id) {
	T_bed_serialconnection result = null;
	Object obj = getFlyweight(Integer.valueOf(id));
	if (obj != null) {
	    result = (T_bed_serialconnection) obj;
	}
	return result;
    }

    public int addSerialConnection(String name, int connId, int connTypeId) {
	int id = 0;
	T_bed_serialconnection obj = createSerialConnection(name, connId,
		connTypeId);
	if (obj != null) {
	    id = obj.getId();
	    putFlyweight(Integer.valueOf(id), obj);
	}
	return id;
    }

    public boolean saveSerialConnection(int id, String name, int connId,
	    int connTypeId) {
	return updateSerialConnection(id, name, connId, connTypeId);
    }

    private void loadSerialConnections() {
	T_bed_serialconnection[] connections = readSerialConnections();
	if (connections != null) {
	    for (int i = 0; i < connections.length; i++) {
		putFlyweight(Integer.valueOf(connections[i].getId()),
			connections[i]);
	    }
	}
    }

    @Override
    protected Object loadFlyweight(Object key) {
	T_bed_serialconnection obj = readSerialConnectionById((Integer) key);
	return obj;
    }

    private T_bed_serialconnection[] readSerialConnections() {
	T_bed_serialconnection[] result = null;
	try {
	    result = T_bed_serialconnectionFactory
		    .listT_bed_serialconnectionByQuery(null, null);
	    if (result == null) {
		result = new T_bed_serialconnection[0];
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_serialconnection readSerialConnectionById(int id) {
	T_bed_serialconnection result = null;
	try {
	    T_bed_serialconnectionCriteria criteria = new T_bed_serialconnectionCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_serialconnection();
	} catch (PersistentException ex) {
	}
	return result;
    }

    private boolean updateSerialConnection(int id, String name, int connId,
	    int connTypeId) {
	boolean result = false;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_serialconnection obj = getSerialConnectionById(id);
		if (obj != null) {
		    T_bed_connecttype connecttype = getConnectType(connTypeId);
		    obj.setName(name);
		    obj.setConnId(connId);
		    obj.setType(connecttype);
		    result = obj.save();
		}
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    public T_bed_serialconnection createSerialConnection(String name,
	    int connId, int connTypeId) {
	T_bed_serialconnection result = null;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_serialconnection obj = new T_bed_serialconnection();
		T_bed_connecttype connecttype = getConnectType(connTypeId);
		obj.setName(name);
		obj.setConnId(connId);
		obj.setType(connecttype);
		obj.save();
		t.commit();
		result = obj;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		// closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_connecttype getConnectType(int id) {
	T_bed_connecttype result = null;
	try {
	    T_bed_connecttypeCriteria criteria = new T_bed_connecttypeCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_connecttype();
	} catch (PersistentException ex) {
	}
	return result;
    }

    public static void main(String[] args) {
	T_bed_serialconnection serialConnection = SerialConnectionDataHandler
		.newInstance().getSerialConnectionById(1);
	if (serialConnection != null) {
	    System.out.println(serialConnection.getName());
	}
	Collection col = SerialConnectionDataHandler.newInstance()
		.getSerialConnections();
	LinkedList<T_bed_serialconnection> connList = SerialConnectionDataHandler
		.newInstance().getSerialConnectionList();
	HashMap connMap = SerialConnectionDataHandler.newInstance()
		.getSerialConnectionMap();

	int i = 0;
	/*
	 * int id =
	 * SerialConnectionDataHandler.newInstance().addSerialConnection(
	 * "deneme d3", 1, 1); if (id > 0) {
	 * System.out.println("addSerialConnection successful:" + id); } else {
	 * System.out.println("addSerialConnection failed"); }
	 * 
	 * SerialConnectionDataHandler.newInstance().saveSerialConnection(id,
	 * "deneme d3____", 1, 1);
	 */
    }
}
